package com.ruoyi.food.controller;


import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.food.object.TRedBlack;
import com.ruoyi.food.service.TRedBlackService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import com.ruoyi.common.core.domain.AjaxResult.Type;


@Controller
@Api(value = "TRedBlackController", tags = "红黑榜controller")
@RequestMapping("/redBlack")
public class TRedBlackController extends BaseController{
	
	@Autowired
	private TRedBlackService tRedBlackService;
	
	private String prefix = "page/week/redBlack";
	
	@GetMapping()
    public String redBlack()
    {
        return prefix+"/redBlack";
    }
	
	/**
     * 新增红黑榜
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    
    @RequiresPermissions("redBlack:add")
    @Log(title = "紅黑榜管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "保存红黑榜", notes = "")
    @ApiParam(name = "tRedBlack", value = "红黑榜对象", required = true, type = "TRedBlack")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult save(@Validated TRedBlack tRedBlack){
        List<TRedBlack> tRedBlacks=new ArrayList<TRedBlack>();
        tRedBlack.setIsValid(1);
        if(tRedBlack.getEnterpriseName()==null || tRedBlack.getEnterpriseName().equals("")) {
        	return new AjaxResult(Type.ERROR,"被检查单位不能为空");
        }else if(tRedBlack.getShopSign()==null||tRedBlack.getShopSign().equals("")) {
        	return new AjaxResult(Type.ERROR,"招牌名称不能为空");
        }else if(tRedBlack.getAddress()==null||tRedBlack.getAddress().equals("")) {
        	return new AjaxResult(Type.ERROR,"地址不能为空");
        }else if(tRedBlack.getEvalTime()==null) {
        	return new AjaxResult(Type.ERROR,"评比时间不能为空");
        }
        tRedBlacks.add(tRedBlack);
        return toAjax(tRedBlackService.saveTRedBlack(tRedBlacks));

    }
    
    /**
     * 查询红黑榜
     * @param tRedBlack
     * @return
     */
    @RequiresPermissions("redBlack:list")
    @ApiOperation(value = "查询红黑榜", notes = "")
    @ApiParam(name = "tRedBlack", value = "红黑榜对象", required = true, type = "TRedBlack")
    @PostMapping("/list")
	@ResponseBody
    public TableDataInfo findTRedBlack(TRedBlack tRedBlack) {
    	startPage();
    	List<TRedBlack> list=new ArrayList<>();
    	list=tRedBlackService.findTRedBlack(tRedBlack);
    	return getDataTable(list);
    }
    
    @ApiOperation(value = "查询全部的红黑榜", notes = "")
    @PostMapping("/listAll")
	@ResponseBody
    public TableDataInfo findAllTRedBlack() {
    	List<TRedBlack> list=new ArrayList<>();
    	list=tRedBlackService.findAllTRedBlack();
    	return getDataTable(list);
    }
    
    
    @GetMapping("/update/{listId}")
    public String update(@PathVariable("listId") Long listId, ModelMap mmap)
    {
    	mmap.put("redBlack", tRedBlackService.findRedBlackById(listId));
        return prefix + "/update";
    }
    
    
    
    /**
     * 修改红黑榜
     * @param tRedBlack
     * @return
     */
    @RequiresPermissions("redBlack:update")
    @ApiOperation(value = "修改红黑榜", notes = "")
    @ApiParam(name = "tRedBlack", value = "红黑榜对象", required = true, type = "TRedBlack")
    @PostMapping("/update")
	@ResponseBody
    public AjaxResult updateRedBlack(@Validated TRedBlack tRedBlack) {
    	 return toAjax(tRedBlackService.updateRedBlack(tRedBlack));
    	
    }
    
    /**
     * 删除红黑榜（逻辑删除）
     * @param listId
     * @return
     */
    @RequiresPermissions("redBlack:delete")
    @ApiOperation(value = "删除红黑榜（逻辑删除）", notes = "")
    @ApiImplicitParam(name = "ids", value = "红黑榜id")
    @PostMapping("/delete")
	@ResponseBody
    public AjaxResult deleteRedBlack(Long ids) {
    	if(ids==null) {
    		return new AjaxResult(Type.ERROR,"红黑榜id不能为空");
    	}
    	 return toAjax(tRedBlackService.deleteRedBlack(ids));
    }

}
